//
//  FCAlongNotesViewController.swift
//  FinancialConsulting
//
//  Created by Max on 2017/10/26.
//  Copyright © 2017年 com.vector. All rights reserved.
//

import UIKit

class FCAlongNotesViewController: FCViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

    @IBOutlet weak var tableView: FCTableView!
    @IBOutlet weak var twoLabel: UILabel!
    var textFiledArr: NSMutableArray!
    var textFiledArrs: NSMutableArray!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.tableFooterView = UIView()
        if self.navigationItem.leftBarButtonItem != nil {
            let btn = UIButton(type: UIButtonType.custom)
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        }
        
        textFiledArr = NSMutableArray.init()
        textFiledArrs = NSMutableArray.init()
        
        
        if FCModelData.instance.isLogin == false {
            
            if let v = FCLoginViewController.create() as? FCLoginViewController {
                
                self.navigationController?.isNavigationBarHidden = true
                v.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(v, animated: true)
            }
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 3
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if indexPath.section == 0 {
    
            return 70
        
        }
        
        return 44
    }

    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            
            return 2
            
        }else if section == 1 {
        
         
            return 4
        
        }else {
            
            return 1
        }

    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        if section == 1 {
            
            return " "
        }else if section == 2 {
        
            return " "
        
        }else {
        
           return ""
        
        }
    }

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
    
        switch indexPath.section {
        
        case 0:
            cell = tableView.dequeueReusableCell(withIdentifier: FCAlongNotesOneCell.name,for: indexPath)
            if let cellOne = cell as? FCAlongNotesOneCell {
           
                cellOne.titleText.tag = indexPath.row
                textFiledArrs.add(cellOne.titleText)
                cellOne.titleText.delegate = self
                if indexPath.row == 0 {
                    cellOne.titleLabel.text = "现金账户"
                    cellOne.describeLabel.text = "现金"
                    cellOne.titleText.placeholder = "请输入金额"
                }else {
                    cellOne.titleLabel.text = "金融账户"
                    cellOne.describeLabel.text = "银行卡"
                    cellOne.titleText.placeholder = "请输入金额"
                }

            }
        case 1:
            cell = tableView.dequeueReusableCell(withIdentifier: FCAlongNotesTwoCell.name,for: indexPath)
            
            if let cellTwo = cell as? FCAlongNotesTwoCell {

                cellTwo.titleText.tag = indexPath.row
                 textFiledArr.add(cellTwo.titleText)
                cellTwo.titleText.delegate = self
                if indexPath.row == 0 {
                     cellTwo.titleText.placeholder = "请输入金额"
                     cellTwo.describeLabel.text = "交通费"
                }else if indexPath.row == 1 {
                 cellTwo.describeLabel.text = "餐费"
                    cellTwo.titleText.placeholder = "请输入金额"
                }else if indexPath.row == 2 {
                 cellTwo.describeLabel.text = "线上支付合计"
                cellTwo.titleText.placeholder = "请输入金额"
                }else  {
                 cellTwo.describeLabel.text = "其它支出"
                 cellTwo.titleText.placeholder = "请输入金额"
                 
                    
                }
            }

        case 2:
            cell = tableView.dequeueReusableCell(withIdentifier: FCAlongNotesTwoCell.name,for: indexPath)
            
            if let cellTwo = cell as? FCAlongNotesTwoCell {
               cellTwo.describeLabel.text = "本月总计支出"
               cellTwo.titleText.isUserInteractionEnabled = false
        
               cellTwo.titleText.text = String(FCModelData.instance.anOone + FCModelData.instance.anOTwo + FCModelData.instance.anOThree + FCModelData.instance.anOFour)

            
            }
          
        
        default:
            break
        }

        return cell
    }
    
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        for textField in textFiledArr {
            let textResult = textField as! UITextField
            if textResult.tag == 0 {
                if let s = textResult.text {
                    if let s1 = Int(s) {
                        
                        FCModelData.instance.anOone = s1
                    }
                }

                twoLabel.text = String(FCModelData.instance.anOone + FCModelData.instance.anOTwo + FCModelData.instance.anOThree + FCModelData.instance.anOFour)
                tableView.reloadData()
            }
            if textResult.tag == 1 {
            
                if let s = textResult.text {
                    if let s1 = Int(s) {
                    
                        FCModelData.instance.anOTwo = s1
                    }
                }
                twoLabel.text = String(FCModelData.instance.anOone + FCModelData.instance.anOTwo + FCModelData.instance.anOThree + FCModelData.instance.anOFour)
                tableView.reloadData()
            }
            if textResult.tag == 2 {
                if let s = textResult.text {
                    if let s1 = Int(s) {
                        
                        FCModelData.instance.anOThree = s1
                    }
                }

                twoLabel.text = String(FCModelData.instance.anOone + FCModelData.instance.anOTwo + FCModelData.instance.anOThree + FCModelData.instance.anOFour)
                tableView.reloadData()
            }
            
            if textResult.tag == 3 {
                if let s = textResult.text {
                    if let s1 = Int(s) {
                        
                        FCModelData.instance.anOFour = s1
                    }
                }

                twoLabel.text = String(FCModelData.instance.anOone + FCModelData.instance.anOTwo + FCModelData.instance.anOThree + FCModelData.instance.anOFour)
                tableView.reloadData()
            }
        }
        
        
        
        
        for textFields in textFiledArrs {
            let textResult = textFields as! UITextField
            if textResult.tag == 0 {
                if let s = textResult.text {
                    if let s1 = Int(s) {
                        
                        FCModelData.instance.anOFive = s1
                    }
                }
                twoLabel.text = String(FCModelData.instance.anOone + FCModelData.instance.anOTwo + FCModelData.instance.anOThree + FCModelData.instance.anOFour)
            
                tableView.reloadData()
            }
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


}
